package cn.didi.feidi.androidorder.fragment.personal;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import org.greenrobot.greendao.async.AsyncOperation;
import org.greenrobot.greendao.async.AsyncOperationListener;
import org.greenrobot.greendao.async.AsyncSession;
import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;

import cn.didi.feidi.androidorder.OrderApplication;
import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.adapter.PersonalCollectionsRecyclerViewAdapter;
import cn.didi.feidi.androidorder.databinding.FragmentPersonalCollectionsBinding;
import cn.didi.feidi.androidorder.fragment.FeidiBaseFragment;
import cn.didi.feidi.androidorder.model.WaterBucket;


/**
 * Created by jinxiong on 2017/8/24.
 */

public class PersonalCollectionsFragment extends FeidiBaseFragment {


    FragmentPersonalCollectionsBinding binding;
    PersonalCollectionsRecyclerViewAdapter adapter;
    AsyncSession asyncSession;

    /**
     * 与activity关联，后面可考虑单例模式
     *
     * @param bundle
     * @return
     */
    public static PersonalCollectionsFragment newInstance(Bundle bundle) {
        PersonalCollectionsFragment fragment = new PersonalCollectionsFragment();
        if (bundle != null)
            fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        adapter = new PersonalCollectionsRecyclerViewAdapter(this);
        asyncSession = OrderApplication.getDaoSession().startAsyncSession();
    }

    @Override
    protected View initFragment(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_personal_collections, container, false);
        return binding.getRoot();
    }

    @Override
    protected void initViews(View rootView) {
        binding.personalCollectionRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        binding.personalCollectionRecyclerView.setAdapter(adapter);

        if (adapter.getItemCount() == 0) {
            binding.collectionPlaceHolder.setVisibility(View.VISIBLE);
        } else {
            binding.collectionPlaceHolder.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initData() {
        asyncSession.setListenerMainThread(new AsyncOperationListener() {
            @Override
            public void onAsyncOperationCompleted(AsyncOperation operation) {


                if (operation.isFailed()) {
                    Toast.makeText(mActivity, "获取收藏列表错误", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (operation.getType() == AsyncOperation.OperationType.QueryList) {

                    ArrayList<WaterBucket> list = (ArrayList<WaterBucket>) operation.getResult();

                    if (list != null && list.size() > 0) {
                        adapter.setCollections(list);

                        if (adapter.getItemCount() == 0) {
                            binding.collectionPlaceHolder.setVisibility(View.VISIBLE);
                        } else {
                            binding.collectionPlaceHolder.setVisibility(View.GONE);
                        }
                    }


                }
            }
        });

        asyncSession.queryList(QueryBuilder.internalCreate(OrderApplication.getDaoSession().getDao(WaterBucket.class)).build());
    }

    @Override
    protected void addListener() {

        binding.personalCollectionSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                if (!binding.personalCollectionSwipeRefreshLayout.isRefreshing()) {
                    binding.personalCollectionSwipeRefreshLayout.setRefreshing(true);
                }


                asyncSession.setListenerMainThread(new AsyncOperationListener() {
                    @Override
                    public void onAsyncOperationCompleted(AsyncOperation operation) {

                        if (binding.personalCollectionSwipeRefreshLayout.isRefreshing()) {
                            binding.personalCollectionSwipeRefreshLayout.setRefreshing(false);
                        }

                        if (operation.isFailed()) {
                            Toast.makeText(mActivity, "刷新失败", Toast.LENGTH_SHORT).show();
                            return;
                        }

                        ArrayList<WaterBucket> list = (ArrayList<WaterBucket>) operation.getResult();

                        adapter.setCollections(list);

                        if (adapter.getItemCount() == 0) {
                            binding.collectionPlaceHolder.setVisibility(View.VISIBLE);
                        } else {
                            binding.collectionPlaceHolder.setVisibility(View.GONE);
                        }


                        Toast.makeText(mActivity, "刷新成功", Toast.LENGTH_SHORT).show();
                    }

                });

                asyncSession.queryList(QueryBuilder.internalCreate(OrderApplication.getDaoSession().getDao(WaterBucket.class)).build());

            }
        });
    }


}
